package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("activities")
@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result list(Integer channel, Integer type, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("列表查询: channel:{}, type:{}, status:{},page:{}, pageSize:{}", channel, type, status, page, pageSize);
        //调用service,分页查询
        PageResult<Activity> pageResult = activityService.list(channel, type, status, page, pageSize);
        return Result.success(pageResult);

    }

    @PostMapping
    public Result add(@RequestBody Activity activity) {
        log.info("添加: {}", activity);
        activityService.add(activity);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除: {}", id);
        activityService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询: {}", id);
        Activity activity = activityService.getById(id);
        return Result.success(activity);
    }
    @PutMapping
    public Result update(@RequestBody Activity activity) {
        log.info("修改: {}", activity);
        activityService.update(activity);
        return Result.success();
    }
   @GetMapping("/type/{type}")
    public Result findByType(@PathVariable Integer type) {
        log.info("查询: {}", type);
        List<Activity> activity = activityService.findByType(type);
        return Result.success(activity);
    }

}
